package com.yugao.fintech.libra.shortlink.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.libra.common.ram.ActionType;
import com.yugao.fintech.libra.common.ram.RamResource;
import com.yugao.fintech.libra.shortlink.model.entity.ShortLink;
import com.yugao.fintech.libra.shortlink.model.rqrs.*;
import com.yugao.fintech.libra.shortlink.service.ShortLinkService;
import com.yugao.fintech.meheco.core.domain.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 短链
 */
@RestController
@RequestMapping("/shortLink")
@Tag(name = "短链")
public class ShortLinkController {
    @Autowired
    private ShortLinkService shortLinkService;

    @Operation(description = "保存-短链")
    @PostMapping("/saveLink")
    @RamResource(action = "sl:shortLink:save", name = "保存短链", type = ActionType.WRITE)
    public ResultVO<?> saveLink(@Validated @RequestBody ShortLinkSaveReq req) {
        return ResultVO.success(shortLinkService.saveLink(req));
    }

    @Operation(description = "分页-短链")
    @GetMapping("/pageLink")
    @RamResource(action = "sl:shortLink:query", name = "查询短链", type = ActionType.READ)
    public ResultVO<IPage<ShortLinkResp>> pageLink(@Validated ShortLinkBasePageReq req) {
        return ResultVO.success(shortLinkService.pageLink(req));
    }

    @Operation(description = "通过id查找-短链")
    @GetMapping("/getLink")
    @RamResource(action = "sl:shortLink:query", name = "查询短链", type = ActionType.READ)
    public ResultVO<ShortLink> getLink(@RequestParam(value = "id") Long id, @RequestParam("groupId") Long groupId) {
        return ResultVO.success(shortLinkService.getLinkById(id, groupId));
    }

    @Operation(description = "更新-短链")
    @PostMapping("/updateLink")
    @RamResource(action = "sl:shortLink:update", name = "更新短链", type = ActionType.WRITE)
    public ResultVO<?> updateLink(@Validated @RequestBody ShortLinkUpdateReq rq) {
        return ResultVO.success(shortLinkService.updateLink(rq));
    }

    @Operation(description = "批量删除-短链")
    @RamResource(action = "sl:shortLink:delete", name = "删除短链", type = ActionType.WRITE)
    @DeleteMapping("/batchDeleteLink")
    public ResultVO<?> batchDeleteLink(@Validated ShortLinkDeleteReq rq) {
        return ResultVO.success(shortLinkService.deleteLinkByIds(rq));
    }
}
